//文件信息输入表单

var departmentComboStore = new Ext.data.Store({
	proxy : new Ext.data.HttpProxy({
		url : 'showDepartment',
		method : 'post'

	}),
	reader : new Ext.data.JsonReader({
		root : 'Department',
		totalProperty : 'totalDepartment',
		id : 'readerDepartment'
	}, [ {
		name : 'cnName'
	}, {
		name : 'enName'
	}, {
		name : 'id'
	}, {
		name : 'parentName'
	}])
});
departmentComboStore.load();

var selected_row=-1;
var fileInfoForm=new Ext.FormPanel({
	title:'输入',
	blankText:'不允许为空',
	labelAlign:'left',
	url:'uploadFiles.action?method=uploadFiles',
	items:[{
			id:'fileName_txt',
			readOnly:true,
			xtype:'textfield',
			name:'fileName',
			fieldLabel:'文件名',
			allowblank:false,
			enableKeyEvents:true,
			listeners : { 
		        "keyup" : function() { 
		        	var c=Ext.getCmp('fileName_txt');
		        	fileInfoList.updateData(selected_row,c.name,c.getValue()); 
		        } 
			}
	    },{
		    id:'project_txt',
			xtype:'textfield',
			name:'project',
			fieldLabel:'项目',
			allowblank:false,
			enableKeyEvents:true,
			listeners : { 
		        "keyup" : function() { 
		        	var c=Ext.getCmp('project_txt');
		        	fileInfoList.updateData(selected_row,c.name,c.getValue()); 
		        } 
			}	    	    	 
	    },{
		    id:'department_cmb',
			xtype:'combo',
			name:'department',
			fieldLabel:'部门',
			allowblank:false,
			emptyText: '请选择',
			mode: 'local',   
	        triggerAction: 'all',   
	        valueField: 'id',   
	        displayField: 'cnName',   
	        editable:false,  
	        selectOnFocus : true,  
	        forceSelection : true,
	        store: departmentComboStore,
			listeners: {
		        select: function(combo, record, index) {
		        	
		        	var c=Ext.getCmp('department_cmb');
		        	fileInfoList.updateData(selected_row,c.name,record.get('cnName'));
		        	fileInfoList.updateData(selected_row,'departmentId',record.get('id'));
	        	}
	        }
	    },{
		    id:'version_txt',
			xtype:'textfield',
			name:'version',
			fieldLabel:'版本',
			allowblank:false,
			enableKeyEvents:true,
			listeners : { 
		        "keyup" : function() { 
		        	var c=Ext.getCmp('version_txt');
		        	fileInfoList.updateData(selected_row,c.name,c.getValue()); 
		        } 
			}		    	    	 
	    }		
	]
//	,
//	buttons:[{
//		text:'提交',
//	    handler:function(){
//	    	fileInfoForm.getForm().submit();
//	    }
//	}]
});

function updateFileInfo(record,store){
	var fileId=record.get('id');
	var fileName=record.get('fileName');
	var department=record.get('departmentId');
	var version=record.get('version');
	var project=record.get('project');
	var type=record.get('type');
	Ext.Ajax.request({
		url: 'updateFileInfo.action?method=updateFileInfo'
			+'&id='+fileId
			+'&fileName='+fileName
			+'&department='+department
	   		+'&version='+encodeURIComponent(version)
	   		+'&project='+encodeURIComponent(project)		
	   		+'&type='+type,
	   	success: function(res,opt){
	   		store.each(function(rec) {
	   			rec.set('state','已提交');
   			});			   		
	   	},
	   	failure: function(){
	   		record.set('state','提交失败');
	   	}
	});
}

//文件列表
var fileInfoList=new Ext.grid.GridPanel({
	enableColumnResize : true,
	border : true,
	enableColumnMove : true,
	enableHdMenu : false,
	stripeRows: true, //显示斑马线
	loadMask: true,
	autoHeight:true,
	autoScroll:true,
	tbar:[{
		text : '提交模型信息',
		iconCls : 'db-icn-upload_',
		handler : function(){
			var store=fileInfoList.getStore();
			//检查信息是否完整
			for(var i=0;i<store.getCount();i++){
				var record=store.getAt(i);
				if(record.get('state')=="未提交"
					&&(record.get('department')=="" 
					||record.get('version')==""
					||record.get('project')==""))
				{
					Ext.Msg.alert('文件信息不完整','请将所有未提交的文件信息补充完整后再次提交！');
					return;
				}
			}
			//提交信息
			var failCount=0;
			for(var i=0;i<store.getCount();i++){
				var record=store.getAt(i);
				if(record.get('state')!="未提交"){
					continue;
				}
				updateFileInfo(record,store);
			}
		},
		scope : this
	}],
	cm : new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(),
	{
		header : '文件名',
		width : 100,
		dataIndex : 'fileName',
		sortable : true,
		id : 'fileName'
	}, {
		header : '项目',
		width : 60,
		dataIndex : 'project',
		sortable : false,
		fixed : true,
		align : 'right'
	}, {
		header : '部门',
		width : 60,
		dataIndex : 'department',
		sortable : false,
		fixed : true,
		renderer : this.formatIcon,
		align : 'center'
	}, {
		header : '版本',
		width : 60,
		dataIndex : 'version',
		sortable : false,
		fixed : true,
		align : 'center'
	}, {
		header : '类型',
		width : 60,
		dataIndex : 'type',
		sortable : false,
		fixed : true,
		align : 'center',
		renderer:function(value, cellmeta, record){
			var typemap=[['0','无模型']
					,['1','流程模型']
					,['2','功能模型']
					,['3','组织模型']
					,['4','业务对象模型']
					,['5','epc模型']
					,['6','权限模型']
					,['7','角色模型']];
			return typemap[value][1];
		}
	}, {
		header : '状态',
		width : 80,
		dataIndex : 'state',
		sortable : false,
		fixed : true,
		align : 'center'
	}]),
	
	ds : new Ext.data.SimpleStore({
		fields : ['id','fileName', 'project', 'departmentId','department','version','type','state']
	}),
	
	listeners : {
		cellclick : {
			fn : function(grid, rowIndex, columnIndex, e) {
				//点击记录，在表单中显示记录信息
				selected_row=rowIndex;
				var record=fileInfoList.getStore().getAt(rowIndex);
				Ext.getCmp('fileName_txt').setValue(record.get('fileName'));
				Ext.getCmp('project_txt').setValue(record.get('project'));
				Ext.getCmp('department_cmb').setValue(record.get('departmentId'));
				Ext.getCmp('version_txt').setValue(record.get('version'));
			},
			scope : this
		}
	},
	//更新列表中数据
	updateData:function(row,field,value){
		if(selected_row<0){
			return;
		}
		var store=fileInfoList.getStore();
		var record=store.getAt(row);
		record.set(field,value);
	}	
});

//文件导入窗口
var uploadFileWin = new Ext.Window({
	title : '导入模型文件',
	width : 500,
	height : 400,
	resizable : false,
	layout : 'fit',
	closeAction: 'hide',             //这个很重要，为了防止关闭窗口后无法打开
	modal:true,
	items : [
	    new Ext.TabPanel({
		border : false,
		id:'file-tab',
		activeTab : 0,
		tabPosition : 'bottom',
		items:[{
			id: 'upload-panel',
			title: '文件列表',
			xtype : 'uploadpanel',
			uploadUrl : 'uploadFiles.action',
			filePostName : 'myUpload', // 这里很重要，默认值为'fileData',这里匹配action中的setMyUpload属性
			flashUrl : 'uploaddialog/js/swfupload.swf',
			fileSize : '500 MB',
			height : 300,
			border : false,
			fileTypes : '*.*', // 在这里限制文件类型:'*.jpg,*.png,*.gif'
			fileTypesDescription : '所有文件',
			postParams : {
				path : 'files\\' // 上传到服务器的files目录下面
			}				
		}]
	})]		
});

//文件信息上传窗口
UpdateFileInfoWin=Ext.extend(Ext.Window,{
	//子类的构造函数，参数为示例化子类时传入自定义配置信息  
    constructor:function(config){  
		var myConfig = Ext.apply(  
		    {  
		        //指定子类的默认属性  
		        
		    },  
		    config          //为实例化My.Control.Window(...)时接收到得新配置项，将覆盖子类默认属性  
		);  
		          
		//调用父类构造函数，用子类配置项去完成初始化  
		UpdateFileInfoWin.superclass.constructor.call(this,myConfig);  
	},  

	      
    //子类重写hide()方法  
    hide:function(){  
        //this.hide();       将无限递归调用^-^  
        var store=fileInfoList.getStore();
        var c=store.getCount();
        var allFinish=true;
        for(var i=0;i<c;i++){
        	if(store.getAt(i).get('state')=='未提交'){
        		Ext.Msg.alert('还有文件信息未提交','请将所有文件信息提交后再关闭窗口！');
        		return;
        	}
        }
        //调用父类中的hide方法显示My.Control.Window  
        
        UpdateFileInfoWin.superclass.hide.call(this);  
    }  
});

var updateFileInfoWin = new UpdateFileInfoWin({
	title : '上传文件信息',
	width : 500,
	height : 400,
	resizable : false,
	layout : 'fit',
	closeAction: 'hide',             //这个很重要，为了防止关闭窗口后无法打开
	autoScroll:true,
	items:new Ext.Panel({
		autoScroll:true,
		title:'文件信息',
		id:'fileInfo-panel',
		items:[
		    fileInfoForm
		    ,fileInfoList
		]
	})
});
